package zl.osimall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zl.osimall.common.bean.Category;
import zl.osimall.common.bean.Goods;
import zl.osimall.common.bean.GoodsPic;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.mapper.CategoryMapper;
import zl.osimall.common.mapper.GoodsMapper;
import zl.osimall.common.mapper.GoodsPicMapper;
import zl.osimall.common.service.GoodsService;

import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsPicMapper goodsPicMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Goods goods) throws SteduException {
        Category category = categoryMapper.selectById(goods.getCategoryId());
        if(category == null || (category != null && category.getParentId() == 0) ){
            throw new SteduException("请选择已存在的二级分类");
        }

        Goods condition= new Goods();
        condition.setCategoryId(goods.getCategoryId());
        condition.setName(goods.getName());
        condition.setColor(goods.getColor());
        condition.setVersion(goods.getVersion());
        boolean flag = goodsMapper.selectByCondition(condition).stream()
                        .anyMatch(item -> item.getName().equals(goods.getName()));
        if(flag){
            throw new SteduException("有重复商品无法添加");
        }

        goodsMapper.insert(goods);
        Integer goodsId = goods.getId();
        List<GoodsPic> picList = goods.getPicList();
        if(picList != null &&!picList.isEmpty()){
            picList.stream().forEach(pic -> pic.setGoodsId(goodsId));
            goodsPicMapper.insert(picList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws SteduException {
        if (goodsMapper.selectById(id) == null){
            throw  new SteduException("该商品不存在，无法删除");
        }
        //TODO 是否有关联订单

        goodsPicMapper.delete(id);
        goodsMapper.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Goods goods) throws SteduException {

        Goods after = goods;
        Goods before = goodsMapper.selectById(goods.getId());
        if(before == null){
            throw new SteduException("该商品不存在，无法修改");
        }

        if(after.getCategoryId() != null){
            Category category = categoryMapper.selectById(after.getCategoryId());
            if(category == null){
                throw new SteduException("该分类必须未存在的子分类");
            }
        }

        Integer afterCategoryId = after.getCategoryId() != null ? after.getCategoryId() : before.getCategoryId();
        String afterName = after.getName() != null ? after.getName() : before.getName();
        String afterColor = after.getColor() != null ? after.getColor() : before.getColor();
        String afterVersion = after.getVersion() != null ? after.getVersion() : before.getVersion();

        Goods condition = new Goods();
        condition.setCategoryId(afterCategoryId);
        condition.setName(afterName);
        condition.setColor(afterColor);
        condition.setVersion(afterVersion);

        boolean flag = goodsMapper.selectByCondition(condition)
                        .stream()
                        .anyMatch(item -> item.getId() != after.getId() && item.getName().equals(afterName));
        if(flag){
            throw new SteduException("与其他商品重复。无法修改");
        }


        goodsMapper.update(goods);
        List<GoodsPic> picList = goods.getPicList();
        if(picList != null && !picList.isEmpty()){
            goodsPicMapper.delete(goods.getId());
            picList.stream().forEach(pic -> pic.setGoodsId(goods.getId()));
            goodsPicMapper.insert(picList);
        }

    }

    @Override
    public Goods selectById(Integer id) throws SteduException {
        Goods goods = goodsMapper.selectById(id);
        if(goods == null){
            throw new SteduException("该商品不存在");
        }
        return goods;
    }

    @Override
    public List<Goods> selectByCount() {

        return goodsMapper.selectByCount();
    }

    @Override
    public PageInfo<Goods> selectByLike(Goods goods, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> goodsList = goodsMapper.selectByLike(goods);
        return new  PageInfo<Goods>(goodsList);
    }

    @Override
    public List<Goods> selectCondition(Goods goods) {
        return goodsMapper.selectByCondition(goods);
    }
}
